﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChuanYe.CountyFireManagement.Api.Models;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ChuanYe.CountyFireManagement.Api.Controllers
{
    /// <summary>
    /// 联动响应
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FireTeamResponseController : ControllerBase
    {
        /// <summary>
        ///   增加
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Add(FireTeamResponseRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                string ip = HttpContext.GetClientIp();
                List<Fireteamresponse> infos = new List<Fireteamresponse>();
                foreach (var item in obj.TeamDepartmentIds)
                {
                    Fireteamresponse info = new Fireteamresponse();
                    info.FireId = obj.FireId;
                    info.TransDatetime =obj.TransDatetime??DateTime.Now;
                    info.Ip = ip;
                    info.UserId = obj.UserId;
                    info.Status = 0;
                    info.TeamDepartmentId = item;
                    infos.Add(info);
                }
                var data = BLL.FireTeamResponseBLL.Instance.Add(infos);
                if (data.FirstParam != null)
                {
                    ControllerHelper.SendMessage(data.FirstParam);
                    var allExpireFreInfo = BLL.FireExpireBLL.Instance.GetUnExpire();
                    MqttHub.fireexpires = allExpireFreInfo;
                }
                result.Status = data.Status;
                result.Message = data.Message;
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/FireTeamResponse/Add Error";
                LogHelper.Error(result.Message,ex);
            }
            return result;
        }




    }
}